﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikeweixin.web.admin.website
{
    public partial class slideadd : weikeweixin.web.admin.cs.adminbase
    {
        t_home_dal homedal = new t_home_dal();
        t_home_model homemodel = new t_home_model();

        public string Name;
        public string Sort;
        public string Description;
        public string Status;
        public string Type;
        public string PicUrl;
        public string Href;
        public string Id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user_id))
            {
                Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                Response.Write("</script>");
                Response.End();
            }

            //根据Id判断是新增还是修改
            if (!IsPostBack && Request.QueryString["Id"] != null)
            {
                Id = Request.QueryString["Id"];
                DataTable dtHomeAd = homedal.GetList("Id='" + Id + "'").Tables[0];
                Name = dtHomeAd.Rows[0]["Name"].ToString();
                Sort = dtHomeAd.Rows[0]["Sort"].ToString();
                Description = dtHomeAd.Rows[0]["Description"].ToString();
                Status = dtHomeAd.Rows[0]["Status"].ToString();
                Type = dtHomeAd.Rows[0]["Type"].ToString();
                PicUrl = dtHomeAd.Rows[0]["PicUrl"].ToString();
                Href = dtHomeAd.Rows[0]["Href"].ToString();
            }
            else {
                Name = "";
                Sort = "0";
                Description = "";
                Status = "1";
                Type = "";
                PicUrl = "styles/images/home-default21.jpg";
                Href = "";
            }

            switch (Request.Form["action"])
            {
                case "bindhomeadd"://商户设置
                    GetHomeAdd();
                    break;
            }
        }


        /// <summary>
        ///添加幻灯片
        /// </summary>
        public void GetHomeAdd()
        {
            homemodel.user_id = int.Parse(user_id);
            homemodel.Name = Request.Form["name"].Trim();
            homemodel.Sort = int.Parse(string.IsNullOrEmpty(Request.Form["sort"].Trim()) ? "0" : Request.Form["sort"].Trim());
            homemodel.Description = Request.Form["description"].Trim();
            homemodel.Status = int.Parse(string.IsNullOrEmpty(Request.Form["is_show"].Trim()) ? "1" : Request.Form["is_show"].Trim());
            homemodel.Type = Request.Form["type"].Trim();
            homemodel.PicUrl = Request.Form["picurl"].Trim();
            homemodel.Href = Href;
            string url = Request.Url.Authority;
            switch (homemodel.Type)
            {
                case "articlelist":
                    homemodel.Href = "http://" + url + "/phone/List?userid=" + user_id + "&cid=" + Request.Form["category_id"];
                    break;
                case "article":
                    homemodel.Href = "http://" + url + "/phone/News?userid=" + user_id + "&nid=" + Request.Form["article"];
                    break;
                case "link":
                    homemodel.Href = Request.Form["link"];
                    break;
                case "tel":
                    homemodel.Href = "tel:"+Request.Form["tel"];
                    break;
            }


            //新增
            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                homemodel.Id = homedal.Add(homemodel);
                Response.Write("{\"errno\":\"0\",\"tip\":\"幻灯片添加成功！\",\"url\":\"slide.aspx\",\"error\":\"\"}");
                Response.End();
            }
            //修改
            else {
                homemodel.Id = int.Parse(Request.Form["id"].Trim());
                bool re = homedal.Update(homemodel);
                Response.Write("{\"errno\":\"0\",\"tip\":\"幻灯片修改成功！\",\"url\":\"slide.aspx\",\"error\":\"\"}");
                Response.End();
            }

        }

    }
}